package com.digitalchina.lxnsy.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.farmTrain.DemandDetailActivity;
import com.digitalchina.lxnsy.activity.farmVideo.VideoDetailActivity;
import com.digitalchina.lxnsy.activity.old.ActivityDetailActivity;
import com.digitalchina.lxnsy.activity.old.FarmingInfoDetailActivity;
import com.digitalchina.lxnsy.activity.old.ShareFarmDetailActivity;
import com.digitalchina.lxnsy.activity.old.ShareShopActivity;
import com.digitalchina.lxnsy.activity.old.TechnologyTrainingVideoDetailNewActivity;
import com.digitalchina.lxnsy.activity.old.TechnologyTrainingVideoSearchActivity;
import com.digitalchina.lxnsy.api.BusinessCollect;
import com.digitalchina.lxnsy.base.BaseV4Fragment;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.CollectVO;
import com.digitalchina.lxnsy.interfaces.ICallBackPos;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.CollectHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.fragment
 * 创建人：郑皓
 * 创建时间：2018/8/8 15:31
 * 修改备注：
 * ================================================
 */
public class CollectFragment extends BaseV4Fragment implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, ICallBackPos {


    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private String type = "";

    private List<CollectVO> voList = new ArrayList<>();

    @Override
    public View setRootView() {
        return inflate(R.layout.fragment_collect);
    }

    @Override
    public void initMember() {

        type = getArguments().getString("type");

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.addItemDecoration(Utils.setDividerDecoration(getActivity(), 10.0f, 0.0f, 0.0f));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new CollectHolder(parent, type,CollectFragment.this);
            }
        });

        onRefresh();

    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                CollectVO vo = (CollectVO) adapter.getAllData().get(position);
                //收藏类型
                switch (type) {
                    case "0":
                        GotoUtil.gotoActivity(getActivity(), ShareFarmDetailActivity.class, "contentId", vo.getId());
                        break;
                    case "1":
                        GotoUtil.gotoActivity(getActivity(), TechnologyTrainingVideoDetailNewActivity.class, "TrainingListVOSc", vo);
                        break;
                    case "2":
                        GotoUtil.gotoActivity(getActivity(), ActivityDetailActivity.class, "contentId", vo.getId());
                        break;
                    case "3":
                        GotoUtil.gotoActivity(getActivity(), FarmingInfoDetailActivity.class, "contentId", vo.getId());
                        break;
                }
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_CALL_OFF_STORE_SUCCESS:
                        showToast("取消收藏成功");
                        onRefresh();
                        break;
                    case MSG.MSG_CALL_OFF_STORE_FIELD:
                        showToast(msg.obj + "");
                        break;
                    case MSG.MSG_GET_USER_STORES_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_USER_STORES_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

        voList = FastJsonUtil.getListBeanNoKey(object.toString(), CollectVO.class);

        if (voList == null)
            return;

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);

        if (voList.size() < limit) {
            adapter.stopMore();
        }
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessCollect.getUserStores(getActivity(), type, page, limit, mHandler);
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            BusinessCollect.getUserStores(getActivity(), type, page, limit, mHandler);
        }
    }

    @Override
    public void clickOk(int pos) {
        CollectVO vo = (CollectVO) adapter.getAllData().get(pos);
        BusinessCollect.callOffStore(getActivity(), vo.getId(), type, mHandler);
    }
}
